/*
 *  $Id: DeathChain.cs 1051 2010-05-18 22:10:41Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Darkness.Spells {
    public class DeathChain: TargetEnchantmentCard<Creature> {
        public DeathChain(): base("001_d_s_death_chain") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Darkness;
            Value = 100;
            Target = CreatureTarget.Both;
        }

        private static readonly Languages.CardPhrase<DeathChain> Already = new Languages.CardPhrase<DeathChain>("already");

        protected override void ManaCostAction(TargetArgs<Creature> args, ElementValues manas) {
            manas.Increment(args.Target.Card.ManaCost);
        }

        protected override bool IsPossibleAction(TargetArgs<Creature> args) {
            if (args.Target.HasAffect<DeathChainAffect>()) {
                args.Player.Game.ErrorNotice(Already.Phrase.GetText());
                return false;
            }
            return true;
        }

        protected override EnchantmentAffect CreateEnchantment() {
            return new DeathChainAffect();
        }

        private class DeathChainAffect: GraveyardEnchantmentAffect<DeathChain> {
            public DeathChainAffect() {
                Add(new Affects.Undying(0));
            }

            public override bool Dispellable {
                get { return false; }
            }

            public override void SetAffect() {
                base.SetAffect();
                LocalSummary.OnDeath += DoDeath;
            }

            public override void ResetAffect() {
                base.ResetAffect();
                LocalSummary.OnDeath -= DoDeath;
            }

            private void DoDeath(AbstractObject obj) {
                Creature cr = obj as Creature;
                if (cr == null) return;
                if (cr.MaxHealth <= 1)
                    cr.ReturnToDeck();
                else {
                    cr.Summary.ChangeDamage(-1);
                    cr.Summary.ChangeMaxHealth(-1);
                }
            }
        }
    }
}
